cd "$pathdata/Beliefs"

use Beliefs_cleaned, clear


preserve
**** Histogram of Cognitive Uncertainty

graph twoway (histogram cognitive_uncertainty if treatment==0, frac gap(15) lcolor(black%20)   fcolor(black%20) width(0.049)),  ///
	title("Belief updating", color(black)) ///
	ytitle("Fraction",) xtitle("Cognitive uncertainty") ///
	graphregion(fcolor(white) lcolor(white)) ///
	ylabel(0(0.04)0.16,glcolor(gs15) ang(h)) xmtick(#20) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	yline(0.16,lstyle(grid)) ///
	xsize(8) ysize(8) ///
	ysc(range(0 0.17) lcolor(none)) xsc(lcolor(none)) yline(.2,lstyle(grid))


restore

* Figure CU split

preserve
egen cu_cutoff = pctile(cognitive_uncertainty) if treatment==0, p(50)
gen uncertain=(cognitive_uncertainty>cu_cutoff)

egen bucket=group(uncertain bayes_round)

gen coef_med=.
gen se_med=.
gen coef_mean=.
gen se_mean=.

levelsof bucket

foreach i in `r(levels)'{
	cap qui qreg2 belief if bucket==`i' & treatment==0, cluster(id)
	cap qui replace coef_med=_b[_cons] if bucket==`i' & treatment==0
	cap qui replace se_med=_se[_cons] if bucket==`i' & treatment==0

	cap qui reg belief if bucket==`i' & treatment==0, cluster(id)
	cap qui replace coef_mean=_b[_cons] if bucket==`i' & treatment==0
	cap qui replace se_mean=_se[_cons] if bucket==`i' & treatment==0
}


collapse (p50) coef_* se_* cognitive_uncertainty bayesian_posterior (count) num = belief if treatment==0, by(uncertain bayes_round)
drop if num < 30

gen upper_med=coef_med+se_med
gen lower_med=coef_med-se_med
gen upper_mean=coef_mean+se_mean
gen lower_mean=coef_mean-se_mean

* Medians
tw (scatter coef_med bayes_round if uncertain==0, msize(medsmall)  ms(o) mcolor(midblue%90)) ///
 	(scatter coef_med bayes_round if uncertain==1, msize(medium) ms(X) mcolor(red%80))  ///
	(rcap upper_med lower_med bayes_round if uncertain==0, msiz(large) lcolor(midblue%90))	///
	(rcap upper_med lower_med bayes_round  if uncertain==1, msiz(large) lcolor(red%80))	///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Posterior belief") title("Belief updating", color(black)) ///
    xtitle("Bayesian posterior") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2 3 5) label(1 "Low cognitive uncertainty") label(2 "High cognitive uncertainty") label(3 "{c 177}1 std. error of median") label(5 "Bayesian prediction") r(2))




* Means
tw (scatter coef_mean bayes_round if uncertain==0, msize(medsmall)  ms(o) mcolor(midblue%90)) ///
 	(scatter coef_mean bayes_round if uncertain==1, msize(medium) ms(X) mcolor(red%80))  ///
	(rcap upper_mean lower_mean bayes_round if uncertain==0, msiz(large) lcolor(midblue%90))	///
	(rcap upper_mean lower_mean bayes_round  if uncertain==1, msiz(large) lcolor(red%80))	///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Posterior belief") title("Belief updating", color(black)) ///
    xtitle("Bayesian posterior") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2 3 5) label(1 "Low cognitive uncertainty") label(2 "High cognitive uncertainty") label(3 "{c 177}1 std. error of mean") label(5 "Bayesian prediction") r(2))


restore

*** Figure Distance to optimal decision

binscatter abs_dist cognitive_uncertainty_wins if treatment==0, ///
		xtitle("Cognitive uncertainty") ytitle("Distance b/w decision and optimal decision") yscale(lstyle(none)) ///
		title("Belief updating", color(black)) ///
		graphregion(color(white)) ///
		ms(+) mcolor(black) lcolor(red%80) ///
		ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
		yline(11.2, lcolor(gs10) lwidth(thin)) ///
		xsize(8) ysize(8) ///
		ysc( lcolor(none)) xsc(lcolor(none)) 
		

spearman abs_dist cognitive_uncertainty if treatment==0



** Treatment comparison

preserve

egen bucket=group(treatment bayes_round)
gen se_belief=.

levelsof bucket

foreach i in `r(levels)'{
	cap qui qreg2 belief if bucket==`i', cl(id)
	cap qui replace se_belief=_se[_cons] if bucket==`i'
}

collapse (p50) belief cognitive_uncertainty se_belief (count) num = belief if round>6, by(treatment bayes_round)
drop if num < 30
gen upper=belief+se_belief
gen lower=belief-se_belief

tw (scatter belief bayes_round if treatment==0, msize(medsmall)  ms(o) mcolor(midblue%90)) ///
 	(scatter belief bayes_round if treatment==1, msize(medium) ms(X) mcolor(red%80))  ///
	(rcap upper lower bayes_round if treatment==0, msiz(large) lcolor(midblue%90))	///
	(rcap upper lower bayes_round  if treatment==1, msiz(large) lcolor(red%80))	///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Posterior belief") title("Complex numbers in belief updating", color(black)) ///
    xtitle("Bayesian posterior") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2 3 5) label(1 "Baseline problem") label(2 "Complex numbers problem") label(3 "{c 177}1 std. error of median") label(5 "Bayesian prediction") r(2))


restore

*** Plot of CU vs. p

preserve

keep if treatment==0 

gen se_cu=.

levelsof bayes_round

foreach i in `r(levels)'{
	cap qreg2 cognitive_uncertainty if bayes_round==`i', cl(id)
	cap replace se_cu=_se[_cons] if bayes_round==`i'
}

collapse (p50) cognitive_uncertainty  se_cu (count) num = cognitive_uncertainty, by(bayes_round)
drop if num < 30

gen upper=cognitive_uncertainty+se_cu
gen lower=cognitive_uncertainty-se_cu

tw 	(scatter cognitive_uncertainty bayes_round, msize(medsmall)  ms(o) mcolor(black)) ///
	(rcap upper lower bayes_round, msiz(large) lcolor(black)),	///
	ytitle("Cognitive Uncertainty") ///
    xtitle("Bayesian posterior") xscale( lcolor(none)) ysc(lcolor(none) r(0 0.5)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) legend(off)


restore


*** CU quartiles

keep if treatment == 0 

xtile z = cognitive_uncertainty, nquantile(4)

forvalues i=1/4{
	reg belief bayesian if z == `i', cl(id)
	estimates store v`i'
}

coefplot (v1, color(black) ciopts(color(black))) (v2, color(black) ciopts(color(black))) (v3, color(black) ciopts(color(black))) (v4, color(black) ciopts(color(black))), drop(_cons) yline(0, lcolor(grey) lpattern(dash)) vertical ysc(lcolor(none)) xscale( lcolor(none))  ylabel(0 (0.2) 0.8, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ysc(r(-0.005,0.04) lcolor(none)) xlabel(0.7 `""CU Q1" "(ave. = 7.5)""' 0.9 `""CU Q2" "(ave. = 25.0)""' 1.1 `""CU Q3" "(ave. = 42.8)""' 1.3 `""CU Q4" "(ave. = 73.1)""') yline(0, lcolor(gs10) lwidth(thin)) grid(glcolor(gs15))  graphregion(color(white) fcolor(white)) ytitle("Regression coefficient and 95% CI") title("Effect of Bayesian probability on beliefs", color(black)) legend(off)


** Across Trial Inconsistency 

use $pathdata/Beliefs/across_trial_inconsistency.dta, clear

gen cognitive_uncertainty_wins=min(cognitive_uncertainty,.6)

binscatter abs_diff_ans cognitive_uncertainty_wins, nquant(10) yscale(lstyle(none)) ///
		xtitle("Cognitive uncertainty") ytitle("Absolute difference b/w posterior beliefs") ///
		title("Belief updating", color(black)) ///
		ms(+) mcolor(black) lcolor(red%80) ///
		graphregion(color(white)) ///
		ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
		yline(7, lcolor(gs10) lwidth(thin)) ///
		ysc( lcolor(none)) xsc(lcolor(none)) 
		


		
reg abs_diff_ans cognitive_uncertainty, cl(id)
reg abs_diff_ans cognitive_uncertainty_wins, cl(id)
pwcorr abs_diff_ans cognitive_uncertainty
spearman abs_diff_ans cognitive_uncertainty


*** Belief Default Manipulation ***

use $pathdata/Beliefs/Beliefs_Default_Manipulation_cleaned.dta, clear

xtile cu_pct=cognitive_uncertainty, n(2)

sum cognitive_uncertainty,d
gen low_cu=(cognitive_uncertainty<=r(p50))

gen default_coef=.

forvalues i=1/2{
	reg belief low_default if cu_pct==`i', cl(id)
	estimates store beliefs`i'
}

coefplot (beliefs1, mcolor(midblue%90) ciopts(color(midblue%90))) (beliefs2, mcolor(red%80) lcolor(red%80)  ciopts(color(red%80))), ///
		ytitle("Treatment effect of low default") drop(_cons) yline(0, lcolor(gs10) lwidth(thin)) vertical ysc(lcolor(none) r(0,12)) ylabel(0(2)12) bgcolor(white) xla(, tlc(none) labcolor(none)) title("Belief updating experiments", color(black)) ///
		xscale( lcolor(none)) ysc(lcolor(none)) ///
		yline(0, lcolor(gs10) lwidth(thin)) ///
		graphregion(color(white)) ///
		ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
		legend(order(2 4) label(2 "Below-median CU") label(4 "Above-median CU"))





